<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Sation Logic Config</title>
    
    <script src="blockly/blockly_compressed.js"></script>
    <script src="blockly/blocks_compressed.js"></script>
    <script src="blockly/zh-hans.js"></script>
    <script src="blockly/javascript_compressed.js"></script>
    <script src="blockly/lua_compressed.js"></script>

    <script src="blockly/myblock/wait_block.js"></script>
    <script src="blockly/myblock/send_value.js"></script>
    <script src="blockly/myblock/device_array.js"></script>
    <script src="blockly/myblock/time_block.js"></script>
    <script src="blockly/myblock/date_block.js"></script>
    <script src="blockly/myblock/weekday_block.js"></script>
    <script src="blockly/myblock/get_current_time.js"></script>
    <script src="js/main.js"></script>

    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/FileSaver.js"></script>

    
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/main.css" rel="stylesheet">
</head>

<body>
  <h2 style="margin-top: 8px;text-align: center;">Sation 可视化逻辑配置工具</h2>
  <!-- <p>
    <button onclick="TransCode()" id="transButton" class="btn btn-success" style="margin-left:10px">转换代码</button>
    <button   class="btn btn-success" style="margin-left: 54%;">刷新表格</button>
  </p> -->

  <div class="container" style="margin: 0;width: 100%;">
    <div class="row">
        <div class=" col-lg-7" > 
            <button onclick="TransCode()" id="transButton" class="btn btn-success btn-style">转换代码</button>
            <span>点击转换代码按钮，将逻辑块转化为代码，显示在右下角的代码编辑框中</span>
        </div>
        <div class=" col-lg-5" > 
            <button   class="btn btn-success btn-style" >刷新表格</button>
            <span>点击刷新表格按钮，从服务器获取最新的设备列表</span>
        </div>
    </div>
    <div class="row"  >
        <div class=" col-lg-7" >  
          <!-- col-md-8 col-xs-6 col-sm-6 -->
            <div id="blocklyDiv" style="height: 880px;box-shadow: 1px 1px 3px 2px #cccccc;"></div>
        </div>
        <div class="col-lg-5" >
          <div style="overflow:auto;height: 415px;box-shadow: 1px 1px 3px 2px #cccccc;">
              <table class="table table-striped table-bordered">
                  <tr>
                      <th>设备</th>
                      <th>功能</th>
                      <th>组地址</th>
                      <th>数据类型(位)</th>
                      <th>值</th>
                      <th>操作</th>
                  </tr>
                  <tr>
                      <td>客厅空调</td>
                      <td>开关</td>
                      <td>1/1/1</td>
                      <td>1</td>
                      <td>1</td>
                      <td><a onclick="insert_block(0)">插入</a></td>
                  </tr>
                  <tr>
                      <td>客厅空调</td>
                      <td>温度</td>
                      <td>1/1/2</td>
                      <td>8</td>
                      <td>24</td>
                      <td><a onclick="insert_block(1)">插入</a></td>
                  </tr>
                  <tr>
                      <td>客厅空调</td>
                      <td>风速</td>
                      <td>1/1/3</td>
                      <td>4</td>
                      <td>1</td>
                      <td><a onclick="insert_block(2)">插入</a></td>
                  </tr>
                  <tr>
                      <td>新风系统</td>
                      <td>开关</td>
                      <td>1/2/1</td>
                      <td>1</td>
                      <td>1</td>
                      <td><a onclick="insert_block(3)">插入</a></td>
                  </tr>
                  <tr>
                      <td>新风系统</td>
                      <td>风速</td>
                      <td>1/2/2</td>
                      <td>4</td>
                      <td>2</td>
                      <td><a onclick="insert_block(4)">插入</a></td>
                  </tr>
                  <tr>
                      <td>卧室照明</td>
                      <td>开关</td>
                      <td>1/3/1</td>
                      <td>1</td>
                      <td>1</td>
                      <td><a onclick="insert_block(5)">插入</a></td>
                  </tr>
                  <tr>
                      <td>卧室照明</td>
                      <td>调光</td>
                      <td>1/3/2</td>
                      <td>16</td>
                      <td>20</td>
                      <td><a onclick="insert_block(6)">插入</a></td>
                  </tr>
                  <tr>
                      <td>氛围灯</td>
                      <td>开关</td>
                      <td>1/4/1</td>
                      <td>1</td>
                      <td>1</td>
                      <td><a onclick="insert_block(7)">插入</a></td>
                  </tr>
                  <tr>
                      <td>氛围灯</td>
                      <td>RGB调光</td>
                      <td>1/4/2</td>
                      <td>16</td>
                      <td>255</td>
                      <td><a onclick="insert_block(8)">插入</a></td>
                  </tr>
                  <tr>
                      <td>书房窗帘</td>
                      <td>开合</td>
                      <td>1/5/1</td>
                      <td>8</td>
                      <td>1</td>
                      <td><a onclick="insert_block(9)">插入</a></td>
                  </tr>
                  <tr>
                      <td>音乐播放器</td>
                      <td>播放/暂停</td>
                      <td>1/6/1</td>
                      <td>2</td>
                      <td>1</td>
                      <td><a onclick="insert_block(10)">插入</a></td>
                  </tr>
              </table>
          </div>
          <div style="height: 50px;">
              <button onclick="SaveCode()" id="runButton" class="btn btn-success btn-style"  >保存代码</button>
              <span>点击保存按钮，将代码保存到本地，代码可通过“转换代码”按钮获取，或在编辑框中直接输入</span>
          </div>
          <div style="box-shadow: 1px 1px 3px 2px #cccccc;">
              <textarea  class="form-control" id="lua_edit"   style="height: 415px;width: 100%;resize:none"></textarea>
          </div>
          <!-- placeholder = "点击转换代码按钮,或直接输入代码:"
          onblur="this.placeholder='点击转换代码按钮,或直接输入代码:\n=========================='"  
          onfocus="this.placeholder=''"  -->
        </div>
    </div>
  </div>



  <xml id="toolbox" style="display: none">
    <category name="判断" colour="%{BKY_LOGIC_HUE}">
      <block type="controls_if"></block>
      <block type="controls_if">
        <mutation else="1"></mutation>
      </block>
    </category>

    <category name="逻辑" colour="%{BKY_LOGIC_HUE}">
      <block type="logic_compare"></block>
      <block type="logic_operation"></block>
      <block type="logic_negate"></block>
      <block type="logic_boolean"></block>
      <block type="logic_null"></block>
    </category>

    <category name="循环" colour="%{BKY_LOOPS_HUE}">
      <block type="controls_repeat_ext">
        <value name="TIMES">
          <shadow type="math_number">
            <field name="NUM">10</field>
          </shadow>
        </value>
      </block>
      
      <block type="controls_whileUntil"></block>
      <block type="controls_flow_statements"></block>
    </category>

    <category name="数值" colour="%{BKY_MATH_HUE}">
      <block type="math_number">
        <field name="NUM">123</field>
      </block>
      <block type="math_arithmetic"></block>
    </category>

    <category name="时间" colour="%{BKY_LISTS_HUE}">
      <block type="wait_seconds"></block>   
      
      <block type="time_block">
          <value name="HOUR">
            <block type="math_number">
               <field name="NUM">0</field>
            </block>
          </value>
          <value name="MINITES">
              <block type="math_number">
                 <field name="NUM">30</field>
              </block>
          </value>
          <value name="SECONDS">
                <block type="math_number">
                   <field name="NUM">0</field>
                </block>
          </value>
      </block>   
      <block type="date_block">
          <value name="YEAR">
              <block type="math_number">
                 <field name="NUM">2020</field>
              </block>
            </value>
            <value name="MONTH">
                <block type="math_number">
                   <field name="NUM">1</field>
                </block>
            </value>
            <value name="DAY">
                  <block type="math_number">
                     <field name="NUM">1</field>
                  </block>
            </value>
      </block>   
      <block type="weekday_block"></block>  
      <block type="get_current_time"></block>   
    </category>

    <category name="文本" colour="%{BKY_TEXTS_HUE}">
    <block type="text"></block>
    <block type="text_print">
      <value name="TEXT">
        <shadow type="text">
          <field name="TEXT">abc</field>
        </shadow>
      </value>
    </block>
    </category>

    <category name="数组" colour="%{BKY_LISTS_HUE}">
      <block type="device_array"></block>
      <block type="send_value"></block>
    </category>

   
    <category name="变量" custom="VARIABLE" colour="%{BKY_VARIABLES_HUE}">
    </category>
  
    <category name="颜色" colour="%{BKY_COLOUR_HUE}">
    <block type="colour_picker"></block>
    <block type="colour_rgb">
      <value name="RED">
        <shadow type="math_number">
          <field name="NUM">100</field>
        </shadow>
      </value>
      <value name="GREEN">
        <shadow type="math_number">
          <field name="NUM">50</field>
        </shadow>
      </value>
      <value name="BLUE">
        <shadow type="math_number">
          <field name="NUM">0</field>
        </shadow>
      </value>
    </block>
    </category>
  </xml>

  <!-- 初始化时显示的块 -->
  <xml  id="startBlocks" style="display: none">
    <!-- <block type="variables_set" id="set_n_initial" inline="true" x="20" y="20">
        <field name="VAR">n</field>
        <value name="VALUE">
          <block type="math_number">
            <field name="NUM">522</field>
          </block>
        </value>
        <next>
            <block type="text_print" id="print" inline="false">
                <value name="TEXT">
                  <block type="variables_get">
                    <field name="VAR">n</field>
                  </block>
                </value>
              </block>
        </next>
      </block> -->
  </xml>

  <script>
     var demoWorkspace = Blockly.inject('blocklyDiv',
    {media: 'blockly/media/',
     toolbox: document.getElementById('toolbox'),
     grid:
            {spacing: 20,
            length: 3,
            colour: '#ccc',
            snap: true}
    });

    Blockly.Xml.domToWorkspace(document.getElementById('startBlocks'),demoWorkspace);
   // Blockly.JavaScript.addReservedWords('exit');  //addReservedWords 添加关键字
    
    var device_id = 1000;
    var lua_edit_area = document.getElementById('lua_edit');
    // lua_edit_area.setAttribute("placeholder" , "这不科学");
    var runButton = document.getElementById('runButton');
    var output_code = ''; // Lua Code
    var edit_code = ''; // Textarea Code

    //输出lua文件的默认代码
    var head_code = "function waitForSeconds(sleep_time)\n" 
                    + "  os.execute('ping -n '..sleep_time..' 127.1 > nul')\n"	
                    + "end\n\n"
                    + "function sendValue(device)\n"
                    + "  print('发送值 '.. device[5]..' 到组地址 '..device[3]..'  值类型: '..device[4]..'Byte')\n"
                    + "  print('控制设备 '..device[1]..'的'..device[2]..'功能')\n"
                    + "  print('')\n"
                    + "end\n\n";                  

    
    demoWorkspace.addChangeListener(function(event) {
      if (!(event instanceof Blockly.Events.Ui)) {
        //resetInterpreter();
        generateCodeAndLoadIntoInterpreter();
      }
    });

    
  </script>
</body>
</html>